package com.weng.bigdata.core.schedule;

/**
 * 调度任务状态
 *
 * @author wengzhonghui
 * @date 10:51 2021/3/23
 */
public enum TaskStatusEnum {
    NOT_STARTED(10, "未启动"),
    RUNNING(20, "执行中"),
    SUCCESS(30, "执行成功"),
    FAILED(40, "执行失败"),
    STOPED(50, "强制停止"),

    WAIT_FOR_START(21, "排队等待中.."),
    WAIT_FOR_PARENT(22, "等待依赖任务执行完成"),
    WAIT_FOR_RETRY(23, "任务重试中..."),
    START_FAIL(41, "任务启动超时失败"),
    WAIT_FOR_PARENT_TIMEOUT(42, "等待父任务超时"),
    UN_KNOWN(-1, "未知");
    Integer value;
    String name;

    private TaskStatusEnum(Integer value, String name) {
        this.value = value;
        this.name = name;
    }

    public static String statusDesc(Integer status){
        for(TaskStatusEnum e : TaskStatusEnum.values()){
            if(e.value.compareTo(status) == 0){
                return e.name;
            }
        }
        return "未知";
    }

    public static TaskStatusEnum getTaskStatusCode(Integer status) {
        if (status >= NOT_STARTED.getValue() && status < RUNNING.getValue()) {
            return NOT_STARTED;
        } else if (status >= RUNNING.getValue() && status < SUCCESS.getValue()) {
            return RUNNING;
        } else if (status >= SUCCESS.getValue() && status < FAILED.getValue()) {
            return SUCCESS;
        } else if (status >= FAILED.getValue() && status < STOPED.getValue()) {
            return FAILED;
        } else if (status >= STOPED.getValue() && status < STOPED.getValue()) {
            return STOPED;
        }
        return NOT_STARTED;
    }

    public Integer getValue() {
        return value;
    }

    public String getName() {
        return name;
    }
}
